package com.example.service.adm.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.context.PageResult;
import com.example.entity.ProductCateDO;
import com.example.entity.ProductDO;
import com.example.exception.exception;
import com.example.mapper.ProductMapper;
import com.example.service.adm.AdmProductService;
import com.example.utils.FileUtil;
import com.example.vo.product.ProductPageReq;
import com.example.vo.product.ProductPageResp;
import com.example.vo.product.ProductReq;
import com.example.vo.product.ProductResp;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author zbxComputer
 * @description 针对表【sys_store_product(商品表)】的数据库操作Service实现
 * @createDate 2024-11-21 12:49:18
 */
@Service
public class AdmProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO>
        implements AdmProductService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private FileUtil fileUtil;

    @Override
    public PageResult<ProductPageResp> findAll(ProductPageReq req) {
        Page<ProductPageResp> page = new Page<>(req.getPage(), req.getSize());
        MPJLambdaWrapper<ProductDO> mpjWrapper = new MPJLambdaWrapper<>();
        mpjWrapper.selectAll(ProductDO.class);
        if (StringUtils.isNotBlank(req.getKeyword())) {
            mpjWrapper.like(ProductDO::getName, req.getKeyword());
        }
        mpjWrapper.leftJoin(ProductCateDO.class, ProductCateDO::getId, ProductDO::getCateId);
        mpjWrapper.selectAll(ProductCateDO.class);
        mpjWrapper.orderByDesc(ProductDO::getCreateTime);
        Page<ProductPageResp> iPage = productMapper.selectJoinPage(page, ProductPageResp.class, mpjWrapper);
        return PageResult.of(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public void delete(Long[] ids) {
        removeByIds(List.of(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProduct(ProductReq req) {
        LambdaQueryWrapper<ProductDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductDO::getId, req.getId());
        ProductDO oldOne = getOne(wrapper);
        if (oldOne == null) {
            throw new exception("商品不存在");
        }
        ProductDO product = getProductDO(req);
        try {
            if (!updateById(product)) {
                throw new exception("更新商品失败");
            }
        } catch (Exception e) {
            throw e;
        }
    }

    private ProductDO getProductDO(ProductReq req) {
        ProductDO product = BeanUtil.copyProperties(req, ProductDO.class);
        if (req.getImage() != null) {
            if (req.getImage() instanceof MultipartFile) {
                product.setImage(fileUtil.saveImgFile((MultipartFile) req.getImage()));
            } else if (req.getImage() instanceof String) {
                product.setImage(fileUtil.saveFile((String) req.getImage()));
            }
        }
        return product;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ProductReq req) {
        ProductDO product = getProductDO(req);
        if (!save(product)) {
            throw new exception("添加商品失败");
        }
    }

    @Override
    public ProductResp findById(Long id) {
        ProductDO product = getById(id);
        if (product == null) {
            return new ProductResp();
        }
        ProductResp resp = BeanUtil.copyProperties(product, ProductResp.class);
        resp.setImage(fileUtil.readImage2Base64(product.getImage()));
        return resp;
    }

    @Override
    public void changeStatusById(Long id) {
        ProductDO product = getById(id);
        if (product == null) {
            throw new exception("商品不存在");
        }
        product.setIsShow(!product.getIsShow());
        if (!updateById(product)) {
            throw new exception("更新失败");
        }
    }
}




